package com.jyw.system.utils;

import com.jyw.common.constant.CacheConstants;
import com.jyw.common.core.domain.entity.SysUser;
import com.jyw.common.core.redis.RedisCache;
import com.jyw.common.utils.StringUtils;
import com.jyw.common.utils.spring.SpringUtils;
import com.jyw.system.service.ISysUserService;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 沈峻
 * @ClassName UserUtils
 * @Description 用户工具类
 * @Date 2023/8/23 16:13
 **/
public class UserUtils {
    /**
     * 用户id转用户名称
     * @param userId (可以是字符串 1,2,3 也可以是Long类型)
     * @return
     */
    public static String userIdToName(Object userId){
        if(ObjectUtils.isEmpty(userId)){
            return null;
        }
        List<SysUser> list = cacheList();
        //判断用户Id是否为字符串
        if(userId instanceof String){
            String uidStr = (String) userId;
            if(!StringUtils.isEmpty(uidStr)){
                return Arrays.stream(uidStr.split(",")).map(data -> {
                    Optional<SysUser> optional = list.stream().filter(user -> user.getUserId().toString().equals(data)).findFirst();
                    if (optional.isPresent()) {
                        return optional.get().getNickName();
                    } else {
                        return data;
                    }
                }).collect(Collectors.joining(","));
            }
        } else if (userId instanceof Long) {
            Long uidLong = (Long) userId;
            Optional<SysUser> optional = list.stream().filter(user -> user.getUserId().equals(uidLong)).findFirst();
            if (optional.isPresent()){
                return optional.get().getNickName();
            }else{
                return userId.toString();
            }
        }
        return null;
    }

    /**
     * 通过id获取用户信息
     * @param id
     * @return
     */
    public static SysUser getUserById(Long id){
        Optional<SysUser> optional = cacheList().stream().filter(data -> id.equals(data.getUserId())).findFirst();
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    /**
     * 清除用户工具类使用的用户缓存
     */
    public static void cleanCache(){
        getRedisCache().deleteObject(CacheConstants.USER_CACHE_KEY);
    }

    /**
     * 获取用户列表缓存
     */
    public static List<SysUser> cacheList(){
        RedisCache redisCache = getRedisCache();
        if (redisCache.hasKey(CacheConstants.USER_CACHE_KEY)) {
            //存在用户缓存列表
            return redisCache.getCacheList(CacheConstants.USER_CACHE_KEY);
        }else {
            //从数据库中索引
            List<SysUser> list = getUserService().list();
            redisCache.setCacheList(CacheConstants.USER_CACHE_KEY,list);
            return list;
        }
    }

    /**
     * 获取用户接口服务
     * @return
     */
    private static ISysUserService getUserService(){
        return SpringUtils.getBean(ISysUserService.class);
    }
    /**
     * 获取Redis操作对象
     */
    private static RedisCache getRedisCache(){
        return SpringUtils.getBean(RedisCache.class);
    }

    /**
     * 通过名字获取ID
     * @param name
     * @return
     */
    public static Long nameToId(String name) {
        if(ObjectUtils.isEmpty(name)){
            return null;
        }
        Optional<SysUser> optional = cacheList().stream().filter(data -> name.equals(data.getNickName())).findFirst();
        if(optional.isPresent()){
            return optional.get().getUserId();
        }
        return null;
    }
}
